package jmine.tec.datadigester.positional.writer;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import jmine.tec.datadigester.positional.meta.PositionalTimestampFieldMetadata;
import bancosys.tec.utils.date.Timestamp;

/**
 * {@link PositionalFieldWriter} para campos do tipo TimeStamp
 * 
 * @author israel.crisanto
 */
public class TimestampPositionalFieldWriter extends AbstractPositionalFieldWriter<PositionalTimestampFieldMetadata> {

    /**
     * Construtor
     * 
     * @param fieldMetadata fieldMetadata
     */
    public TimestampPositionalFieldWriter(PositionalTimestampFieldMetadata fieldMetadata) {
        super(fieldMetadata);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object read(String line) {
        PositionalTimestampFieldMetadata meta = this.getFieldMetadata();
        String data = line.substring(meta.start(), meta.start() + meta.size());
        DateFormat df = new SimpleDateFormat(meta.format());
        try {
            Date parsed = df.parse(data);
            return new Timestamp(parsed.getTime());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
